import requests
import openpyxl
from lxml import etree

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36',
}
wb = openpyxl.Workbook()
ws = wb.active
ws.append(['分类', '首字母', '歌手名'])


def singer_request():
    html = etree.HTML(requests.get(url=url, headers=headers).text)
    return html


def type_all():
    html = singer_request()

    # 获取歌手分类
    type_list = html.xpath('//div[@class="blk"]/ul[@class="nav f-cb"]/li')
    return type_list


def singer_list():
    type_list = type_all()
    # 获取歌手分类
    for t in type_list:
        type_href = t.xpath('./a/@href')[0]
        type_name = t.xpath('./a[@href]/text()')
        page = requests.get(url=('https://music.163.com' + type_href), headers=headers).text
        art_html = etree.HTML(page)
        # body = art_html.xpath('//ul[@id="initial-selector"]')[0]  # 整体的块
        # 获取首字母列表
        # w_href = body.xpath('./li/a/@href')
        # w_first = body.xpath('./li/a[@href]/text()')
        # for href in w_href:
        # page2 = requests.get(url=('https://music.163.com' + href), headers=headers).text
        #     singer_page = etree.HTML(page2)
        singers = art_html.xpath('.//ul[@id="m-artist-box"]')[0]
        s_list = singers.xpath('./li/p/a[@class="nm nm-icn f-thide s-fc0"]/text()')
        for n in s_list:
            # ws.append(type_name + w_first + [n])
            save(type_name, n)


def save(t_name, s_name):
    d = {}
    d['小标题'] = t_name
    d['歌手'] = s_name
    with open('网易云歌手.txt', 'a', encoding='utf-8') as fp:
        fp.write(str(d)+'\n')


if __name__ == '__main__':
    url = 'https://music.163.com/discover/artist/'
    singer_list()










